package org.nashua.tt151.utils;

public class List {
    protected Object[] vals;
    public List(){
        this(new Object[0]);
    }
    public List(Object[] vals){
        this.vals=vals;
    }
    public void add(Object val) {
        Object[] v = vals;
        vals = new Object[v.length+1];
        System.arraycopy(v,0,vals,0,v.length);
        vals[v.length]=val;
    }
    public void clear(){
        vals=new Object[0];
    }
    public boolean contains(Object val){
        return indexOf(val)!=-1;
    }
    public Object get(int i) throws IndexOutOfBoundsException {
        return vals[i];
    }
    public boolean getBoolean(int index){
        return Genericless.toBoolean(get(index));
    }
    public double getDouble(int index){
        return Genericless.toDouble(get(index));
    }
    public float getFloat(int index){
        return Genericless.toFloat(get(index));
    }
    public int getInt(int index){
        return Genericless.toInt(get(index));
    }
    public String getString(int index){
        return get(index).toString();
    }
    public int indexOf(Object val) {
        for (int i=0;i<vals.length;i++)
            if (vals[i].equals(val))
                return i;
        return -1;
    }
    public void insert(int index, Object val){
        index=index>=vals.length?vals.length:index;
        Object[] v = vals;
        vals = new Object[v.length+1];
        System.arraycopy(v, 0, vals, 0, index);
        vals[index]=val;
        if (index!=v.length) System.arraycopy(v,index+1,vals,index,v.length-index);
    }
    public void remove(int index) throws ArrayIndexOutOfBoundsException{
        if (index>=vals.length) throw new ArrayIndexOutOfBoundsException();
        Object[] v = vals;
        vals = new Object[v.length-1];
        System.arraycopy(v, 0, vals, 0, index);
        System.arraycopy(v,index+1,vals,index,v.length-index);
    }
    public void remove(Object val){
        if (contains(val)) {
            Object[] v = vals;
            vals = new Object[v.length-1];
            System.arraycopy(v, 0, vals, 0, indexOf(val));
            System.arraycopy(v,indexOf(val)+1,vals,indexOf(val),v.length-indexOf(val));
        }
    }
     public Object[] toArray(){
        return vals;
    }
    public static class Tuple extends List {
        public void add(Object o){}
        public void clear(){}
        public void insert(int i, Object o){}
        public void remove(int i) throws ArrayIndexOutOfBoundsException {}
        public void remove(Object o){}
    }
}